<!--
 * @Description: 预警巡查列表
 * @Version: 1.0
 * @Autor: younghxp
 * @Date: 2021-12-13 10:24:29
 * @LastEditors: younghxp
 * @LastEditTime: 2021-12-22 17:08:19
-->
<template>
  <div class="container" ref="mainCon">
    <div class="mainCon">
      <el-row class="toolbar searchInputWidth" style="padding-bottom: 0">
        <el-form :inline="true" :model="filterField" ref="searchConditions">
          <el-col :lg="8" :xl="6">
            <el-form-item label="项目名称" class="row-padding-bottom">
              <el-input
                placeholder="请输入项目名称"
                clearable
                v-model="filterField.projectName"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="区域" class="row-padding-bottom">
              <el-cascader
                v-model="areaId"
                :options="areasOptions"
                @change="getAreaId"
                clearable
                filterable
                :props="{ checkStrictly: true }"
                :append-to-body="false"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="行业" class="row-padding-bottom">
                <el-select
                  v-model="filterField.industryId"
                  filterable
                  placeholder="请选择"
                  clearable
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in industryOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="预警类型" class="row-padding-bottom">
                <el-select
                  v-model="filterField.typeId"
                  clearable
                  filterable
                  placeholder="请选择"
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in typeOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="处理状态" class="row-padding-bottom">
                <el-select
                  v-model="filterField.status"
                  clearable
                  filterable
                  placeholder="请选择"
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in handleStatusOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="是否处罚" class="row-padding-bottom">
                <el-select
                  v-model="filterField.isPunished"
                  clearable
                  filterable
                  placeholder="请选择"
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in isPunishOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="处理方式" class="row-padding-bottom">
                <el-select
                  v-model="filterField.handleType"
                  clearable
                  filterable
                  placeholder="请选择"
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in handleTypeOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="预警时间" class="row-padding-bottom">
                <timeSelection
                  :startTime="filterField.effectiveStartDate"
                  :endTime="filterField.effectiveEndDate"
                  @getStartTimeValue="getStartTimeValue"
                  @getEndTimeValue="getEndTimeValue"
                ></timeSelection>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="开工状态" class="row-padding-bottom">
                <el-select
                  v-model="filterField.projectStatus"
                  filterable
                  placeholder="请选择"
                  clearable
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in statusOptions"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item
                label="网格中心处理情况"
                class="row-padding-bottom gridLabel"
              >
                <el-select
                  v-model="filterField.gridAlertStatus"
                  filterable
                  placeholder="请选择"
                  clearable
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in gridHandleOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </template>
          <el-col :lg="8" :xl="6">
            <el-form-item label="">
              <a
                @click="toggleAdvanced"
                style="margin-left: 40px; color: #409eff; cursor: pointer"
                class="btnColor"
              >
                {{ advanced ? '收起' : '展开' }}
                <i
                  :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                ></i>
              </a>
              <el-button
                type="primary"
                icon="icon iconfont iconchazhao1"
                @click="searchList"
                class="searchBtn"
              >
                <span>搜索</span>
              </el-button>
              <exportBtn
                :dataLen="tableList.length"
                :filterField="computedfilterField"
                api="api-a/ipqc/alert/gridAlertExport"
                responseType="get"
              ></exportBtn>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <el-table
          ref="tableBox1"
          :max-height="tableHeight"
          tooltip-effect="dark"
          :data="tableList"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          v-loading="loading"
          stripe
          style="border: 1px solid #dfe6ec"
        >
          <el-table-column label="项目名称" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.projectName"
                columnName="projectName"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="industryName"
            label="行业"
            align="center"
            :formatter="formatTd"
            width="120"
          ></el-table-column>
          <el-table-column
            prop="areaDetailDesc"
            label="区域"
            align="center"
            :formatter="formatTd"
            width="230"
          ></el-table-column>
          <el-table-column
            prop="typeName"
            label="预警类型"
            align="center"
            :formatter="formatTd"
            width="220"
          ></el-table-column>
          <el-table-column label="预警原因" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.reason"
                :columnName="'reason'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="alertTime"
            label="预警时间"
            align="center"
            :formatter="formatTd"
            width="140"
          ></el-table-column>
          <el-table-column
            prop="isPunishedValue"
            label="处罚情况"
            align="center"
            :formatter="formatTd"
            width="100"
          ></el-table-column>
          <el-table-column
            prop="statusDesc"
            label="状态"
            align="center"
            :formatter="formatTd"
            width="100"
          ></el-table-column>
          <el-table-column
            prop="gridAlertStatusDesc"
            label="网格中心处理情况"
            align="center"
            :formatter="formatTd"
            width="160"
          ></el-table-column>
          <el-table-column
            prop="inspectionTime"
            label="网格中心处理时间"
            align="center"
            :formatter="formatTd"
            width="180"
          ></el-table-column>
          <el-table-column
            prop="handleTypeValue"
            label="处理方式"
            align="center"
            width="120"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column label="操作" align="left" fixed="right" width="120">
            <template slot-scope="scope">
              <el-link type="primary" @click="handleAlert(scope.row, 'detail')"
                >查看详情</el-link
              >
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关信息"></emptyTable>
          </div>
        </el-table>
        <Pagination
          ref="pagination"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
          @getpageSize="getpageSize"
          @getpageNo="getpageNo"
        ></Pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex'
export default {
  name: 'WarningPatrolList',
  data() {
    return {
      advanced: false,
      hiddleToolBar: false,
      tableHeight: null,
      tableList: [], //表格数据
      requestUrl: 'api-a/ipqc/alert/gridAlertList',
      areaId: '',
      typeOptions: [], //预警类型
      handleStatusOptions: [], //处理状态
      isPunishOptions: [], //是否处罚
      handleTypeOptions: [], //处理方式
      gridHandleOptions: [], //网格中心处理情况
      computedfilterField: {
        serverArea: '',
      },
      filterField: {
        projectName: '',
        areaId: '',
        industryId: '',
        typeId: '',
        status: '',
        isPunished: '',
        handleType: '',
        effectiveStartDate: '',
        effectiveEndDate: '',
        projectStatus: '',
        gridAlertStatus: '',
        serverArea: '', //服务部署区域
      },
      pageSize: 20,
      pageNo: 1,
      isShow: false, //列表加载
      loading: true,
    }
  },
  computed: {
    ...mapState(['areasOptions', 'industryOptions', 'statusOptions']),
  },
  created() {
    this.getOptionsData({
      url: 'api-bs/base/dicitem/findbytype',
      type: 'CS_ALERT_TYPE',
      optionsData: 'typeOptions',
    }) //预警类型下拉框
    this.getOptionsData({
      url: 'api-bs/base/dicitem/findbytype',
      type: 'CS_ALERT_STATE',
      optionsData: 'handleStatusOptions',
    }) //处理状态下拉
    this.getOptionsData({
      url: 'api-a/cs/alert/expand/ispunish',
      optionsData: 'isPunishOptions',
    }) //是否处罚下拉
    this.getOptionsData({
      url: 'api-bs/base/dicitem/findbytype',
      type: 'CS_ALERT_HANDLE_TYPE',
      optionsData: 'handleTypeOptions',
    }) //处理方式下拉框
    this.getOptionsData({
      url: 'api-a/cs/alert/expand/gridAlertStatus',
      optionsData: 'gridHandleOptions',
    }) //网格中心处理情况
    this.filterField.serverArea = this.$store.state.areaInfoArr.areaId
    this.computedfilterField.serverArea = this.$store.state.areaInfoArr.areaId
  },
  mounted() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
    })
    window.addEventListener('scroll', this.handleScroll, true)
    let that = this
    this.$nextTick(function () {
      let tableHeightCur = this.$refs.tableBox1.$el.clientHeight
      let tableTop = this.$refs.tableBox.getBoundingClientRect().top
      if (tableHeightCur + tableTop > window.innerHeight) {
        this.tableHeight =
          this.$refs.mainCon.getBoundingClientRect().height - 86
      } else {
        document.getElementsByClassName('tableBoxCur').length &&
          (document.getElementsByClassName('tableBoxCur')[0].style.height =
            window.innerHeight - tableTop - 28 + 'px')
      }
      window.onresize = function () {
        if (!that.$refs.tableBox1) {
          return
        }
        let tableHeightCur = that.$refs.tableBox1.$el.clientHeight
        let tableTop = that.$refs.tableBox.getBoundingClientRect().top
        if (tableHeightCur + tableTop > window.innerHeight) {
          that.tableHeight =
            that.$refs.mainCon.getBoundingClientRect().height - 86
        } else {
          document.getElementsByClassName('tableBoxCur').length &&
            (document.getElementsByClassName('tableBoxCur')[0].style.height =
              window.innerHeight - tableTop - 40 + 'px')
        }
      }
    })
  },
  activated() {
    window.addEventListener('scroll', this.handleScroll, true)
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
      this.$refs.tableBox1.doLayout()
    })
  },
  deactivated() {
    let that = this
    window.removeEventListener('scroll', that.handleScroll, true)
  },
  beforeDestroy() {
    let that = this
    window.removeEventListener('scroll', that.handleScroll, true)
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleScroll(e) {
      this.$nextTick(function () {
        if (
          this.$refs.tableBox &&
          this.$refs.tableBox.getBoundingClientRect().top < 128
        ) {
          // this.tableHeight = this.$refs.mainCon.getBoundingClientRect().height - 84
          this.tableHeight = window.innerHeight - 246

          if (document.getElementsByClassName('tableBoxCur').length) {
            document.getElementsByClassName('tableBoxCur')[0].style.height =
              null
          }
          this.hiddleToolBar = true
        } else {
          this.hiddleToolBar = false
        }
      })
    },
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    getStartTimeValue(val) {
      if (val) {
        this.filterField.effectiveStartDate = val
      } else {
        this.filterField.effectiveStartDate = ''
      }
    },
    getEndTimeValue(val) {
      if (val) {
        this.filterField.effectiveEndDate = val
      } else {
        this.filterField.effectiveEndDate = ''
      }
    },
    /**
     * @description: 字典下拉
     * @param {*} ,optionsData对应下拉名
     * @return {*}
     */
    getOptionsData({ url, type, optionsData } = {}) {
      this.axios({
        method: 'post',
        url,
        data: { type },
      })
        .then((response) => {
          if (!response.data.hasError) {
            if (optionsData != 'isPunishOptions') {
              this[optionsData] = response.data.result
            } else {
              this[optionsData] = response.data
            }
          } else {
            this.$message.error(response.data.errorMsg)
          }
        })
        .catch((error) => {
          if (error.response) {
            this.$message.error(error.response.data)
          }
        })
    },
    /**
     * @description: 跳转预警详情
     * @param {*} row
     * @param {*} type
     * @return {*}
     */
    handleAlert(row, type) {
      const query = {
        alertId: row.id,
        isShow: 'yes',
        type,
        fromPath: '/home/gridPatrol/patrolTask',
      }
      this.$router.push({
        path: '/home/project/constructAlertDetail',
        name: 'constructAlertDetail',
        query,
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/gridPatrol/patrolTask',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    //获取选中的区域最后一级id
    getAreaId() {
      var areaId = this.areaId[this.areaId.length - 1]
        ? this.areaId[this.areaId.length - 1]
        : ''
      this.filterField.areaId = areaId
    },
    getTableList(data) {
      this.tableList = data
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    // 搜索
    searchList() {
      this.computedfilterField = JSON.parse(JSON.stringify(this.filterField))
      this.computedfilterField.projectName = encodeURIComponent(
        this.computedfilterField.projectName
      )
      var that = this
      this.$nextTick(function () {
        that.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
      })
    },
  },
}
</script>

<style lang="less" scoped>
.mainCon {
  padding-top: 24px;
}
/deep/.gridLabel {
  .el-form-item__label {
    line-height: 18px;
  }
}
@import '../../../static/css/filterArea.less';
</style>
